#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int strcmp1 (const char*, const char*);
int strcmp2 (const char*, const char*);
int strncmp1 (const char*, const char*, size_t);
int strncmp2 (const char*, const char*, size_t);

int main(int argc, const char *argv[])
{
    string str1, str2;

    cout << "Please Input Two Strings (Input EOF to Quit):" << endl;

    while (cin >> str1 >> str2)
    {
        cout << "strcmp1 returns " << strcmp1 (str1.c_str(), str2.c_str())
             << endl;
        cout << "strcmp2 returns " << strcmp2 (str1.c_str(), str2.c_str())
             << endl;
        cout << "strcmp returns " << strcmp (str1.c_str(), str2.c_str())
             << endl << endl;
		cout << "strncmp1 (n = 3) returns " << strncmp1 (str1.c_str(), str2.c_str(), 3)
             << endl;
        cout << "strncmp2 returns " << strncmp2 (str1.c_str(), str2.c_str(), 3)
             << endl;
        cout << "strncmp returns " << strncmp (str1.c_str(), str2.c_str() ,3)
             << endl << endl;
        cout << "Please Input Two Strings (Input EOF to Quit):" << endl;
    }

    return 0;
}


int strcmp1 (const char* str1, const char* str2)
{
    for (int i = 0; ; i++)
    {
        if (str1[i] == str2[i] && str1[i] == '\0')
            return 0;
        if (str1[i] != str2[i])
            return str1[i] - str2[i];
    }
}

int strcmp2 (const char *str1, const char *str2)
{
    while (*str1 && *str2 && *str1 == *str2)
    {
        str1++;
        str2++;
    }

    if (*str1 == *str2 && !*str1)
        return 0;

    return *str1 - *str2;
}

int strncmp1 (const char *str1, const char *str2, size_t size)
{
	for (int i = 0; i < size; i++)
    {
        if (str1[i] == str2[i] && str1[i] == '\0')
            return 0;
        if (str1[i] != str2[i])
            return str1[i] - str2[i];
    }

	return 0;
}


int strncmp2 (const char *str1, const char *str2, size_t size)
{
    for (int i = 0; i < size; i++)
    {
        if (*(str1 + i) == *(str2 + i) && *(str1 + i) == '\0')
            return 0;
        if (*(str1 + i) != *(str2 + i))
            return *(str1 + i) - *(str2 + i);
    }

	return 0;
}
